// components/order-item/index.js
var app = getApp();
Component({
    properties: {
        orders: Object,
        type: String,
        baseUrl: String
    },
    data: {
        state: undefined,
        url: app.globalData.url,
        index: undefined,
        expire: undefined,
        state: undefined
    },
    methods: {
        // 初始化订单状态
        initState() {
            if (this.data.index) {
                clearInterval(this.data.index)
            }
            let state;
            let orders = this.data.orders
            if (orders.state === 0) {
                state = "待付款"
                let expire = new Date(orders.createDate).getTime() + 15 * 60 * 1000 - new Date().getTime()
                this.setData({ expire })
                let index = setInterval(() => {
                    this.countDown()
                }, 1000)
                this.setData({ index })
            } else if (orders.state === 1) {
                state = "已付款"
            } else {
                state = "交易取消"
            }
            this.setData({ state })
        },
        // 取消订单
        cancle() {
            this.triggerEvent('cancle', { id: this.data.orders.id, msg: '取消' })
        },
        // 立即付款
        pay() {
            let list = []
            list.push(this.data.orders)
            let total = this.data.orders.goods.killPrice
            wx.navigateTo({
                url: '/pages/order/index?list=' + JSON.stringify(list) + "&total=" + total,
            })
        },
        // 删除订单
        del() {
            this.triggerEvent('cancle', { id: this.data.orders.id, msg: '删除' })
        },
        // 再次购买
        again() {
            if (this.data.orders.state == 1) {
                wx.showModal({
                    title: '提示',
                    content: '每个用户限购一件',
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            } else {
                // 加入购物车
                wx.showLoading({
                    title: '请稍后...',
                })
                const that = this
                // 发送到后端生成订单
                wx.request({
                    url: that.data.url + '/orders/addCar',
                    data: {
                        userid: that.data.user.id,
                        goodsId: that.data.orders.goodsNumber
                    },
                    success(res) {
                        if (res.data.code == 1) {
                            wx.hideLoading({
                                success: (res) => {
                                    wx.showToast({
                                        title: '添加成功',
                                        success() {
                                            wx.reLaunch({
                                                url: '/pages/car/index',
                                            })
                                        }
                                    })
                                },
                            })

                        } else {
                            wx.showModal({
                                title: '错误',
                                content: res.data.msg,
                                success(res) { }
                            })
                        }
                    }
                })
            }

        },
        // 格式化倒计时时间
        timeFormatter(time) {
            if (time < 0) {
                return "";
            } else {
                time = time / 1000;
                let second = Math.floor(time % 60)
                let min = Math.floor(time / 60);  // 分
                return (min >= 10 ? min : "0" + min) + "分" +
                    (second >= 10 ? second : "0" + second) + "秒"
            }
        },
        // 倒计时
        countDown() {
            let expire = this.data.expire - 1000
            if (expire < 0) {
                // 修改订单状态为已过期
                const that = this
                wx.request({
                    url: that.data.url + '/orders/edit',
                    data: {
                        id: this.data.orders.id,
                        state: 2
                    },
                    method: "POST",
                    success(res) {
                        if (res.data.code == 1) {
                            clearInterval(that.data.index)
                        }
                    }
                })
            }
            this.setData({ expire })
            this.setData({ countDown: this.timeFormatter(expire) })
        },
        info() {
            let list = []
            list.push(this.data.orders)
            let that = this
            wx.navigateTo({
                url: '/pages/order/index?list=' + JSON.stringify(list) + "&total=" + that.data.orders.goods.newPrice + "&type=info",
            })
        }
    },
    lifetimes: {
        attached: function () {
            const user = wx.getStorageSync("user")
            if (user) {
                this.setData({ user })
            } else {
                // 发送请求登录
            }
            this.initState()
        },
        moved() {
            this.initState()
        }
    },

})
